<?php

namespace App\Http\Controllers\Admin\Finance;

use App\Http\Controllers\Controller;
use App\Models\CustomerModel;
use App\Models\finance\AgentContractModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class AgentContract extends Controller
{

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $agent_id = $request->input('agent_id', '');
        $create_start = $request->input('create_start', '');
        $create_end = $request->input('create_end', '');

        $query = AgentContractModel::query()->with('currency');


        if($agent_id){
            $query = $query->where('agent_id', $agent_id);
        }

        if($create_start && $create_end){
            $query = $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'agent_name' => $item->agent_name,
//                'agent_id' => $item->agent_id,
                'sn' => $item->sn,
                'start_date' => $item->start_date,
                'deposit' => $item->deposit,
                'deposit_amount' => $item->deposit_amount,
                'currency_id' => $item->currency_id,
                'currency_code' => $item->currency->short_code ?? '',
                'take_date' => $item->take_date,
                'take_person' => $item->take_person,
                'sign_person' => $item->sign_person,
                'remarks' => $item->remarks,
                'file' => $item->file,
                'create_user' => $item->create_admin ? $item->create_admin->staff->name : '',
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }


    /**
     * Notes:添加
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $validator = Validator::make($params, [
            'agent_name' => 'required',
        ], [
            'agent_name.required' => '请填写服务提供商',
        ]);

        if ($validator->fails()) {
            $err = $validator->errors()->first();
            return $this->error($err);
        }

        $model = new AgentContractModel();
        $model->agent_name = $params['agent_name'];
        $model->sn = $params['sn'] ?? '';
        $model->start_date = $params['start_date'] ?? '';
        $model->deposit = $params['deposit'] ?? 0;
        $model->deposit_amount = $params['deposit_amount'] ?? null;
        $model->currency_id = $params['currency_id'] ?? null;
        $model->take_date = $params['take_date'] ?? '';
        $model->take_person = $params['take_person'] ?? '';
        $model->sign_person = $params['sign_person'] ?? '';
        $model->remarks = $params['remarks'] ?? '';
        $model->file = $params['file'] ?? '';
        $model->created_admin_id = $auth->id;
        $model->save();

        return $this->success();
    }

    public function update(Request $request, $id){
        $auth = $request->get('auth');
        $params = $request->all();

        $validator = Validator::make($params, [
            'agent_id' => 'required',
        ], [
            'agent_id.required' => '请选择服务提供商',
        ]);

        if ($validator->fails()) {
            $err = $validator->errors()->first();
            return $this->error($err);
        }

        $model = AgentContractModel::find($id);
        if (!$model) {
            return $this->error('数据不存在');
        }

        $model->agent_name = $params['agent_name'];
        $model->sn = $params['sn'] ?? '';
        $model->start_date = $params['start_date'] ?? '';
        $model->deposit = $params['deposit'] ?? 0;
        $model->deposit_amount = $params['deposit_amount'] ?? null;
        $model->currency_id = $params['currency_id'] ?? null;
        $model->take_date = $params['take_date'] ?? '';
        $model->take_person = $params['take_person'] ?? '';
        $model->sign_person = $params['sign_person'] ?? '';
        $model->remarks = $params['remarks'] ?? '';
        $model->file = $params['file'] ?? '';
        $model->save();

        return $this->success();
    }


    public function delete(int $id){
        $model = AgentContractModel::find($id);
        if(!$model) {
            return $this->error('记录不存在');
        }
        $model->delete();
        return $this->success();
    }


}
